package test.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;

public class BatchInsert {
	
	static String url = "jdbc:mysql://192.168.5.33/goods?characterEncoding=UTF-8";
	static String username = "licai";
	static String password = "123456";
	static int pageSize = 1000;
	
	static Executor executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
	
	public static void batchInsert() {
		DruidPlugin dp = new DruidPlugin(url, username, password);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setDevMode(true);
		arp.setShowSql(true);
		dp.start();
		arp.start();
		
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr");
		int page = 1;
		while(true) {
			String sql = "select * from goods limit ";
			
			int start = ((page++)-1) * pageSize;
			
			sql += start + "," + pageSize;
			
			List<Record> list = Db.find(sql);
			if(list.size()<1000) break;
			
			List<SolrInputDocument> docs = new ArrayList<>();
			for(Record record : list) {
				SolrInputDocument doc = new SolrInputDocument();
				doc.addField("id", record.getInt("id"));
				doc.addField("name", record.getStr("name"));
				doc.addField("category", record.getStr("category"));
				doc.addField("brand", record.getStr("brand"));
				doc.addField("jd_price", record.getBigDecimal("jd_price"));
				doc.addField("market_price", record.getBigDecimal("market_price"));
				doc.addField("company", record.getStr("company"));
				doc.addField("evaluate_count", record.getInt("evaluate_count"));
				doc.addField("ctime", record.getDate("ctime"));
				docs.add(doc);
			}
			try {
				client.add("core1", docs);
				client.commit("core1");
			} catch (SolrServerException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void deleteAll() {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr");
		try {
			client.deleteByQuery("core1", "*:*");
			client.commit("core1");
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void query() {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr");
		try {
			QueryResponse qr = client.query("core1", new SolrQuery("name:小米,电信"));
			System.out.println(qr);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
//		deleteAll();
//		batchInsert();
		query();
	}

}
